package com.sx.zongzhi.adapter;


import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.sx.zongzhi.R;
import com.sx.zongzhi.bean.ContactBean;

import java.util.List;

/**
 * Created by LQun_1 on 2017/11/28
 * 层级
 */
public class LevelAdapter extends BaseExpandableListAdapter {

    private Context mContext;
    private LayoutInflater mInflater = null;
    private List<ContactBean> contactBeen;

    public LevelAdapter(Context ctx, List<ContactBean> list) {
        mContext = ctx;
        mInflater = LayoutInflater.from(ctx);
        contactBeen = list;

    }

    /**组的数量*/
    @Override
    public int getGroupCount() {
        return contactBeen.size();
    }

    /**当前组子控件的数量*/
    @Override
    public int getChildrenCount(int groupPosition) {
        return contactBeen.get(groupPosition).rows.size();
    }

    /**当前父控件的数据*/
    @Override
    public String getGroup(int groupPosition) {
        return contactBeen.get(groupPosition).orgType;
    }

    /**子控件的数据*/
    @Override
    public ContactBean getChild(int groupPosition, int childPosition) {

        return contactBeen.get(groupPosition).rows.get(childPosition);

    }

    /**当前组的id*/
    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    /**子控件的id*/
    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
                             View convertView, ViewGroup parent) {
        ViewGroupHolder holder;
        if (convertView == null) {
            holder = new ViewGroupHolder();
            convertView = mInflater.inflate(R.layout.item_text, null);
            holder.tv_Name= (TextView) convertView.findViewById(R.id.tv_Content);
            convertView.setTag(holder);
        }else{
            holder = (ViewGroupHolder) convertView.getTag();
        }
        holder.tv_Name.setText(getGroup(groupPosition));
        holder.tv_Name.setPadding(80,20,20,20);
        return convertView;
    }

    @Override
    public View getChildView(final int groupPosition, final int childPosition,
                             boolean isLastChild, View convertView, ViewGroup parent) {
        final ViewHolder holder;
            if (convertView == null) {
                holder = new ViewHolder();
                convertView = mInflater.inflate(R.layout.item_contact_list, null);
                holder.checkBox= (TextView) convertView.findViewById(R.id.checkbox);
                holder.imageView= (ImageView) convertView.findViewById(R.id.iv_img);
                convertView.setTag(holder);
            }else{
                holder = (ViewHolder) convertView.getTag();
            }
            if (getChild(groupPosition,childPosition).isSelect()){
                holder.imageView.setImageResource(R.mipmap.img_selected);
            }else {
                holder.imageView.setImageResource(R.mipmap.img_default);
            }
            holder.checkBox.setText(getChild(groupPosition,childPosition).getName());
            holder.imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (getChild(groupPosition,childPosition).isSelect()){
                        getChild(groupPosition,childPosition).setSelect(false);
                        holder.imageView.setImageResource(R.mipmap.img_default);
                    }else {
                        getChild(groupPosition,childPosition).setSelect(true);
                        holder.imageView.setImageResource(R.mipmap.img_selected);
                    }
                }
            });
        return convertView;
    }
    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        /* 很重要：实现ChildView点击事件，必须返回true */
        return true;
    }

    class ViewHolder{
        TextView checkBox;
        ImageView imageView;
    }

    class ViewGroupHolder{
        TextView tv_Name;
    }
}
